package day12.dynamicPrograming;

import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Runner {
    private static List <String> list = new ArrayList<>();

    static{
        try {
            /*
            相对路径
            Runner类对象,getResource()
            用相对路径，来获得一个完整文件路径
            相对路径:
                "/"这个类的存放目录
                "cfg.txt"
             */
            Class<Runner> c = Runner.class;
            URL resource = c.getResource("cfg.txt");
            String path = resource.getPath();
            path = URLDecoder.decode(path,"UTF-8");
            //System.out.println(path);
            BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                new FileInputStream(path), "UTF-8"));
            String line;
            while((line = in.readLine()) != null){
                if(line.startsWith("#")|| line.length() == 0){
                    continue;
                }
                list.add(line);
            }
            in.close();
            //System.out.println(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void lanuch() {
        for (String str: list) {
            try {
                //获得类对象
                String[] element = str.split(",");
                //System.out.println(Arrays.toString(element));
                Class<?> c = Class.forName(element[0]);
                Object instance = c.newInstance();
                //System.out.println(c.getName());
                //获得调用方法
                Method m = c.getDeclaredMethod(element[1].trim(), String.class);
                //System.out.println(c.getName()+" "+m.getName());
                Object o = m.invoke(instance, c.getName());
                System.out.println(o);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    @Override
    public String toString() {
        return super.toString();
    }
}
